﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using trangbaocao.Models;
using System.Collections;

namespace trangbaocao.Controllers
{
    public class Manage_DhsxController : Controller
    {
        //
        // GET: /Quanly_Dhsx/
        private Manage_DhsxModels manage_dhsx = new Manage_DhsxModels();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        public ActionResult Index()
        {
            List<Dhsx> listDhsx = manage_dhsx.getAllDhsx();
            return View(listDhsx);
        }
        public ActionResult Device_Stations()
        {
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            List<Device_Stations_Parament_Using> list = manage_dhsx.dhsx_stations_pra_us();
            return View(list);
        }

        [HttpPost]
        public ActionResult Fill_Stations()
        {
            string trungtam_id = Request.Form["main_stations_id"];
            string tram_id = Request.Form["stations_id"];
            string sql = "";
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            if (trungtam_id != "" && trungtam_id != null && IsNumber(trungtam_id))
                sql = " and a.main_stations_id =" + Convert.ToInt32(trungtam_id);
            if (tram_id != "" && tram_id != null && IsNumber(tram_id))
                sql = " and a.stations_id =" + Convert.ToInt32(tram_id);
            List<Device_Stations_Parament_Using> list = manage_dhsx.dhsx_stations_pra_us_where(sql);
            return View("Device_Stations", list);
        }
        private bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        public ActionResult Parameter_Dhsx(int device_id)
        {
            List<Paramenter_Base_Dhsx> list_paramenter_base = manage_dhsx.getParamentBase(device_id);
            ViewData["dhsx_id"] = device_id;
            ViewBag.NameDhsx = manage_dhsx.getNameDhsx(device_id);
            return View(list_paramenter_base);
        }
        public ActionResult Del_Parameter_Dhsx_Base(int pra_id, int dhsx_id)
        {
            manage_dhsx.DeleteParameterBaseDhsx(pra_id);
            return RedirectToAction("Parameter_Dhsx", new { device_id = dhsx_id });
        }
        public ActionResult Add_Parameter_Base(int dhsx_id)
        {
            ViewBag.dhsx_id = dhsx_id;
            ViewBag.NameDhsx = manage_dhsx.getNameDhsx(dhsx_id);
            return View();
        }
        public ActionResult Edit_Parameter_Base(int para_id)
        {
            Paramenter_Base_Dhsx para_base = manage_dhsx.getByParamenter_Base_id(para_id);
            ViewBag.dhsx_id = para_base.device_id;
            ViewBag.NameDhsx = manage_dhsx.getNameDhsx(para_base.device_id);
            return View(para_base);
        }
        [HttpPost]
        public ActionResult Edit_Parameter_Base(Paramenter_Base_Dhsx para_base)
        {
            if (ModelState.IsValid)
            {
                manage_dhsx.UpdateParameterBaseDhsx(para_base);
                TempData["msg"] = "Bạn đã cập nhật thông tin thành công!";
                return RedirectToAction("Parameter_Dhsx", new { device_id = para_base.device_id });
            }
            else
            {
                TempData["msg"] = "Bạn đã nhập sai dữ liệu !";
                ViewBag.dhsx_id = para_base.device_id;
                return View();
            }
        }
        [HttpPost]
        public ActionResult Add_Parameter_Base(Paramenter_Base_Dhsx dhsxbase, int dhsx_id)
        {
            Dhsx dsl = manage_dhsx.getDhsxById(dhsx_id);
            if (ModelState.IsValid)
            {
                if (manage_dhsx.checkExist(dhsxbase.name_paramenter, dhsx_id) > 0)
                {
                    TempData["msg"] = "Tham số của thiết bị đã tồn tại!";
                    return RedirectToAction("Add_Parameter_Base", new { dhsx_id = dhsx_id });
                }
                else
                {
                    dhsxbase.device_id = dsl.device_id;
                    dhsxbase.main_sort_device_id = dsl.main_sort_device_id;
                    dhsxbase.sort_device_id = dsl.sort_device_id;
                    dhsxbase.manufactorys_id = dsl.manufactorys_id;
                    dhsxbase.pra_meba_sort_dv_id = 0;
                    manage_dhsx.AddParameterBaseDhsx(dhsxbase);
                    TempData["msg"] = "Bạn đã cập nhật thành công!";
                    return RedirectToAction("Parameter_Dhsx", new { device_id = dhsx_id });
                }
            }
            else
            {
                TempData["msg"] = "Bạn đã nhập sai dữ liệu !";
                ViewBag.dhsx_id = dhsx_id;
                return View();
            }
        }
        public ActionResult Parameter_Using_Dhsx(int device_stations_id)
        {
            List<Paramenter_Using_Dhsx> list = manage_dhsx.getParamenterUsingDhsx(device_stations_id);
            ViewBag.device_stations_id = device_stations_id;
            ViewBag.NameDeviceStation = manage_dhsx.getNameDeviceStation(device_stations_id);
            return View(list);
        }
        public ActionResult Add_Parameter_Using(int device_stations_id)
        {

            ViewBag.device_stations_id = device_stations_id;
            Device_Stations_Parament_Using dvs = manage_dhsx.dhsx_stations_pra_us_id(device_stations_id);
            ViewBag.device_id = dvs.device_id;
            ViewBag.NameDeviceStation = manage_dhsx.getNameDeviceStation(device_stations_id);
            List<Paramenter_Base_Dhsx> para = manage_dhsx.getByParamenter_Base_device_id(Convert.ToInt32(dvs.device_id.ToString()));
            ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
            return View();
        }
        [HttpPost]
        public ActionResult Add_Parameter_Using(Paramenter_Using_Dhsx pra, int device_stations_id)
        {
            Device_Stations device_station = manage_dhsx.getDeviceStationsById(device_stations_id);
            if (ModelState.IsValid)
            {
                if (manage_dhsx.checkPraUsingExist(pra.paramenter_base_id, device_stations_id) > 0)
                {
                    TempData["msg"] = "Tham số của thiết bị đã tồn tại!";
                    return RedirectToAction("Add_Parameter_Using", new { device_stations_id = device_stations_id });
                }
                else
                {
                     pra.device_id = device_station.device_id;
                        pra.main_sort_device_id = device_station.main_sort_device_id;
                        pra.sort_device_id = device_station.sort_device_id;
                        pra.manufactorys_id = device_station.manufactorys_id;
                        pra.device_stations_id = device_station.device_station_id;
                        pra.date_setup = DateTime.Now.ToString("dd/MM/yyyy");
                        pra.date_sys = DateTime.Now.ToString("dd/MM/yyyy");
                        manage_dhsx.AddParamenterUsingDhsx(pra);
                        TempData["msg"] = "Bạn đã cập nhật thành công!";
                        return RedirectToAction("Parameter_Using_Dhsx", new { device_stations_id = device_stations_id });
                   
                }
            }
            else
            {
                ViewBag.device_stations_id = device_stations_id;
                Device_Stations_Parament_Using dvs = manage_dhsx.dhsx_stations_pra_us_id(device_stations_id);
                ViewBag.device_id = dvs.device_id;
                List<Paramenter_Base_Dhsx> para = manage_dhsx.getByParamenter_Base_device_id(Convert.ToInt32(dvs.device_id.ToString()));
                ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
                TempData["msg"] = "Bạn đã nhập sai số liệu!";
                return View();   
            }
        }
        public ActionResult Delete_Parameter_Using(int id, int device_stations_id)
        {
            manage_dhsx.DeleteParamenterUsingDhsx(id);
            ViewBag.device_stations_id = device_stations_id;
            return RedirectToAction("Parameter_Using_Dhsx", new { device_stations_id = device_stations_id });
        }
        public ActionResult Edit_Parameter_Using(int parid)
        {
            Paramenter_Using_Dhsx pra = manage_dhsx.getParameterUsingById(parid);
            List<Paramenter_Base_Dhsx> para = manage_dhsx.getList_ByParamenter_Base_id(pra.paramenter_base_id);
            ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
            ViewBag.device_stations_id = pra.device_stations_id;
            ViewBag.NameDeviceStation = manage_dhsx.getNameDeviceStation(pra.device_stations_id);
            ViewBag.device_id = pra.device_id;
            return View(pra);
        }
        [HttpPost]
        public ActionResult Edit_Parameter_Using(Paramenter_Using_Dhsx par, int device_stations_id)
        {
            if (ModelState.IsValid)
            {
                
                    par.date_setup = DateTime.Now.ToString("dd/MM/yyyy");
                    par.date_sys = DateTime.Now.ToString("dd/MM/yyyy");
                    manage_dhsx.Update_Parabase_Using_Dhsx(par);
                    TempData["msg"] = "Bạn đã cập nhật thành công!";
                    return RedirectToAction("Parameter_Using_Dhsx", new { device_stations_id = device_stations_id });
               
            }
            else
            {
                ViewBag.device_stations_id = device_stations_id;
                Device_Stations_Parament_Using dvs = manage_dhsx.dhsx_stations_pra_us_id(device_stations_id);
                ViewBag.device_id = dvs.device_id;
                List<Paramenter_Base_Dhsx> para = manage_dhsx.getByParamenter_Base_device_id(Convert.ToInt32(dvs.device_id.ToString()));
                ViewBag.Parabase = new SelectList(para, "paramenter_base_id", "name_paramenter");
                TempData["msg"] = "Bạn đã nhập sai số liệu!";
                return View();
            }
        }
        public ActionResult Device_Configuration_Report_Index()
        {
            return View("~/Views/Manage_Dhsx/Report/Device_Configuration_Report_Index.cshtml");
        }
    }
}
